<?php
/**
 * File: rank_change.php
 *
 * This file accepts the mock ajax calls to update the rank of
 * selected classes.  This file also accepts mock ajax calls
 * to get the user's classes.
 */
/**
 *
 */
include 'lib/config.php';
require_once 'lib/mockajaxfbconfig.php';
include 'iHackyFunctions.php';

if(isset($_REQUEST['userClassesInput'])) {

	$conn = getDbConn();
	$pageNum = 1;
	$rowsPerPage = 25;
	if(isset($_REQUEST['page'])){
		$pageNum = $_REQUEST['page'];
	}

	$offset = ($pageNum - 1) * $rowsPerPage;

	$qryGetClassesCount = "SELECT *
	FROM CLASS_USERS
	WHERE facebook_id = $user";
	 
	$rowCount = mysql_query($qryGetClassesCount, $conn) or DIE(mysql_error()."Error at qryGetClassesCount");
	$numRows = mysql_num_rows($rowCount);
	$maxPage = ceil($numRows/$rowsPerPage);

	$queryGetClasses = "SELECT CLASS.class_detail,
	CLASS_USERS.facebook_id, CLASS_USERS.time_start,
	CLASS_USERS.time_last, CLASS_USERS.time_used,
	CLASS_USERS.class_rank, CLASS_USERS.class_id
	 
	FROM CLASS, CLASS_USERS
	WHERE CLASS.class_id = CLASS_USERS.class_id
	AND CLASS_USERS.facebook_id = $user
	ORDER BY CLASS_USERS.class_rank DESC
	LIMIT $offset, $rowsPerPage;";

	$result = mysql_query($queryGetClasses, $conn) or DIE(mysql_error()."Error at queryGetClasses");

	if($pageNum == $maxPage){
		$lastItem = $offset + ($numRows - (($pageNum - 1) * $rowsPerPage));
	}
	else{
		$lastItem = ($offset + $rowsPerPage);
	}
	$offset++;



	echo "

	<br><font color=\"#666666\">
	You have $numRows classes associated with you.<br>
	These are classes you have used that were found in the Hackystat sensorbase and your
	default rank is determined by the number of dev events associated with that class.
	You can manually change your rank and you can
	set your rank to 0 so it doesn't show up in searches.
	</font><br><br>
            ";

	echo  "<div align=\"center\">
	<b><font color=\"#3B5998\">
	Displaying results: $offset - $lastItem<br>
	</font></b>
	</div>";
	 
	while ($row = mysql_fetch_array($result)) {

		$classID = $row['class_id'];
		$className = $row['class_detail'];
		$classRank = $row['class_rank'];
		 
		echo " <div align=\"left\">
		<form name=\"changeRankForm\" method=\"post\" action=\"rank_change.php\">
		<input type=\"hidden\" name=\"id\" value=\"$user\"/>
		<input type=\"hidden\" name=\"class\" value=\"$classID\"/>
		<font color=\"#666666\">$className</font>
		</div>
		<div align=\"left\">
		<select name=\"rank\">";
		for( $i = 0; $i <= 5; $i++) {
			if ($classRank == $i) {
				echo "<option value=\"$i\" selected=\"selected\"> $i
				</option>";
			}
			else {
				echo "<option value=\"$i\"> $i </option>";
			}
		}
		echo "  </select>
                      </div>
                      <div align=\"left\">
                        <input type=\"submit\" value=\"Change Rank\">                  
                      </div>
                      </form>";
	}

	echo "<div align=\"center\">
	<b><font color=\"#666666\">
	Page $pageNum of $maxPage
	</font></b><br>
	</div>";

	if($pageNum == 1 && ($numRows > $rowsPerPage)){
		$newPageNum = $pageNum + 1;
		echo '<br><br>';

		echo "<div align=\"center\">
		<form id=\"first_page\">
		<input name=\"userClassesInput\" type=\"hidden\" value=\"dummy\">
		<input name=\"page\" type=\"hidden\" value=\"$newPageNum\">
		<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/rank_change.php\"
		clickrewriteform=\"first_page\"
		clickrewriteid=\"content\"
		clicktoshow=\"loadIcon\">
		Next
		<img src=\"http://www.ikoaguild.com/ihacky/images/forwardarrow.png\" align=\"top\">
		</a>
		</form>
		</div>";
		 
	}
	else if($pageNum < $maxPage){
		echo '<br><br>';
		$previousPageNum = $pageNum - 1;
		echo "<div align=\"center\">
		<table>
		<tr>
		<td>
		 
		<form id=\"pre_page\">
		<input name=\"userClassesInput\" type=\"hidden\" value=\"dummy\">
		<input name=\"page\" type=\"hidden\" value=\"$previousPageNum\">

		<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/rank_change.php\"
		clickrewriteform=\"pre_page\"
		clickrewriteid=\"content\"
		clicktoshow=\"loadIcon\">
		<img src=\"http://www.ikoaguild.com/ihacky/images/reversearrow.png\" align=\"top\">
		Previous
		</a>
		</form>

		</td>";
		$nextPageNum = $pageNum + 1;
		echo "<td>

		<form id=\"next_page\">
		<input name=\"userClassesInput\" type=\"hidden\" value=\"dummy\">
		<input name=\"page\" type=\"hidden\" value=\"$nextPageNum\">
		<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/rank_change.php\"
		clickrewriteform=\"next_page\"
		clickrewriteid=\"content\"
		clicktoshow=\"loadIcon\">
		Next
		<img src=\"http://www.ikoaguild.com/ihacky/images/forwardarrow.png\" align=\"top\">
		</a>
		</form>
		 
		</td>
		</tr>
		</table>
		</div>";
	}
	else if($pageNum == $maxPage && ($numRows > $rowsPerPage)){
		echo '<br><br>';
		$newPageNum = $pageNum - 1;
		echo "<div align=\"center\">
		<form id=\"last_page\">
		<input name=\"userClassesInput\" type=\"hidden\" value=\"dummy\">
		<input name=\"page\" type=\"hidden\" value=\"$newPageNum\">

		<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/rank_change.php\"
		clickrewriteform=\"last_page\"
		clickrewriteid=\"content\"
		clicktoshow=\"loadIcon\">
		<img src=\"http://www.ikoaguild.com/ihacky/images/reversearrow.png\" align=\"top\">
		Previous
		</a>
		</form>
		</div>";
	}
	echo '
              <div align="center">
                 <img src="http://www.ikoaguild.com/ihacky/images/ajax-loader.gif" id="loadIcon"
                 style="display: none;"/>
              </div>';
	closeDb($conn);
	exit;
}

printTitle();
printDashboard();
printBanner($user);
printFbmlTabs(2);
if(isset($_REQUEST['id'])){

	$userId = $_REQUEST['id'];
	$classID = $_REQUEST['class'];
	$classRank = $_REQUEST['rank'];

	$conn = getDbConn();

	$queryChangeRank = "UPDATE CLASS_USERS SET class_rank = '$classRank'
	WHERE facebook_id = $userId
	AND class_id = $classID;";
	 
	mysql_query($queryChangeRank, $conn) or DIE(mysql_error()."Error at queryChangeRank");

	closeDb($conn);

	echo "<a href=\"profile.php\">Return to your Profile</a>";
	exit;
}


?>